package com.itheima.treemap;

import java.util.TreeMap;
import java.util.function.BiConsumer;

public class A03_TreeMapDemo3 {
    public static void main(String[] args) {
        //定义字符串
        String s = "aaabbbbbssgggg";
        //创建集合
        TreeMap<Character, Integer> tm = new TreeMap<>();

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            //选择
            if (tm.containsKey(c)) {
                int count = tm.get(c);
                count++;
                //将自增之后的结果添加到集合
                tm.put(c, count);
            } else {
                //不存在
                tm.put(c, 1);
            }
        }

        //创建集合 拼接字符串
        StringBuilder sb = new StringBuilder();
        tm.forEach(( key,  value)-> sb.append(key).append("(").append(value).append(")"));

        //打印输出
        System.out.println(sb);
    }
}
